package cn.edu.szpt.qqdemov3;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import cn.edu.szpt.qqdemov3.adapters.QQFragmentAdapter;
import cn.edu.szpt.qqdemov3.beans.QQContactBean;
import cn.edu.szpt.qqdemov3.fragments.QQContactFragment;
import cn.edu.szpt.qqdemov3.fragments.QQMessageFragment;
import cn.edu.szpt.qqdemov3.fragments.QQPluginFragment;

public class MainActivity extends AppCompatActivity {
    private ViewPager2 vp;
    private QQFragmentAdapter adapter;
    private List<Fragment> fragmentList;
    private BottomNavigationView bottomNavigationView;
    public static QQContactBean loginedUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = findViewById(R.id.vp);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);

        fragmentList = new ArrayList<>();
        initialData();
        adapter = new QQFragmentAdapter(getSupportFragmentManager(),getLifecycle(),fragmentList);

        vp.setAdapter(adapter);


        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.menuitem_message:
                        vp.setCurrentItem(0,false);
                        break;
                    case R.id.menuitem_contact:
                        vp.setCurrentItem(1,false);
                        break;
                    case R.id.menuitem_plugin:
                        vp.setCurrentItem(2,false);
                }

                return false;
            }
        });

        vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                MenuItem item = bottomNavigationView.getMenu().getItem(position);
                item.setChecked(true);
                fragmentList.get(position).getView().requestFocus();
            }
        });
    }

    private void initialData(){
        fragmentList.clear();
        QQMessageFragment f1 = new QQMessageFragment();
        QQContactFragment f2 = new QQContactFragment();
        QQPluginFragment f3 = new QQPluginFragment();
        fragmentList.add(f1);
        fragmentList.add(f2);
        fragmentList.add(f3);
    }
}